<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GridManage
 *
 * @author Administrator
 */
//error_reporting(E_ALL);
//ini_set('display_errors', '1');
class Warehouse extends CI_Controller
{

    var $gp;

    public function __construct()
    {
        parent::__construct();
        $this->load->service('wms/warehouse_list_service');
        $this->load->service('wms/inventory_service');
        $this->load->service('wms/inventory_on_order_service');
        $this->load->service('wms/api_service');
        $this->gp = json_decode(file_get_contents('php://input'), true);
        $authorized = $this->api_service->checkAuthorized($this->gp);
    }

    public function getWarehouseCode()
    {
        $rs = $this->warehouse_list_service->get_active_warehouse();
        echo json_encode($rs);
    }

    function getInventory()
    {


        $skuArray = $this->gp['data']['sku'];
        $whs = $this->gp['data']['whsCode'];
        if (is_array($skuArray))
        {
            $rs = $this->inventory_service->get_available_by_array($skuArray, $whs);
            echo json_encode($rs);
        }
        else
        {
            echo '{"error":"sku is null"}';
        }
    }

    function getEnbledShop()
    {
        $product_type_id = $this->gp['data']['type_id'];
        $product_brand_id = $this->gp['data']['brand_id'];
        $product_model_id = $this->gp['data']['model_id'];
        $rs = $this->inventory_service->get_enbled_shop_by_product_type($product_type_id, $product_brand_id, $product_model_id);
        echo json_encode($rs);
    }

    function lockInventory()
    {
        $lstProduct = $this->gp['data'];
        $checkWhere = array(
            'order_id' => $lstProduct[0]['orderId']
        );

        $check = $this->inventory_on_order_service->get_on_track($checkWhere);
        if ($check)
        {
            echo '{"error":"order is already locked"}';
            exit;
        }
        $skuArray = array();
        $time = time();
        foreach ($lstProduct as $productInfo)
        {
            //检查每个产品的可用库存，一个库存不够不能锁定
            $checkAvailabel = $this->inventory_service->get_available($productInfo['sku'], $productInfo['whsCode']);
            if ($checkAvailabel < $productInfo['quantity'])
            {
                echo '{"error":"Inventory shortage"}';
                exit;
            }
            $one['order_id'] = $productInfo['orderId'];
            $one['sku'] = $productInfo['sku'];
            $one['whs_code'] = $productInfo['whsCode'];
            $one['quantity'] = $productInfo['quantity'];
            $one['in_or_out'] = 'out';
            $one['type'] = '1';
            $one['created_time'] = $time;
            $skuArray[] = $one;
        }
        if (count($skuArray))
        {
            $rs = $this->inventory_on_order_service->set_lst_t($skuArray);
            echo $rs;
        }
        else
        {
            echo '{"error":"lstProduct is null"}';
            exit;
        }
    }

}

?>
